/*
 * bsp_adc.c
 *
 *  Created on: 2025 Jan 23
 *      Author: SFLY
 */
#include "bsp_adc.h"


extern __interrupt void adca1_isr(void);

void Bsp_ADC_Init(void)
{
        ASysCtl_disableTemperatureSensor();
        DEVICE_DELAY_US(100U);
        SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_ADCA);
        SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_ADCB);
        SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_ADCC);
        DEVICE_DELAY_US(100U);

        // Configure external reference as 3.3V
        ADC_setVREF(ADCC_BASE, ADC_REFERENCE_EXTERNAL, ADC_REFERENCE_3_3V);
        ADC_setVREF(ADCB_BASE, ADC_REFERENCE_EXTERNAL, ADC_REFERENCE_3_3V);
        ADC_setVREF(ADCA_BASE, ADC_REFERENCE_EXTERNAL, ADC_REFERENCE_3_3V);
        DEVICE_DELAY_US(100U);
        // Set main clock scaling factor (50MHz max clock for the ADC module)
        ADC_setPrescaler(ADCA_BASE, ADC_CLK_DIV_6_0);
        ADC_setPrescaler(ADCB_BASE, ADC_CLK_DIV_6_0);
        ADC_setPrescaler(ADCC_BASE, ADC_CLK_DIV_6_0);

        // set the ADC interrupt pulse generation to end of conversion
        ADC_setInterruptPulseMode(ADCA_BASE, ADC_PULSE_END_OF_CONV);
        ADC_setInterruptPulseMode(ADCB_BASE, ADC_PULSE_END_OF_CONV);
        ADC_setInterruptPulseMode(ADCC_BASE, ADC_PULSE_END_OF_CONV);

        // enable the ADCs
        ADC_enableConverter(ADCA_BASE);
        ADC_enableConverter(ADCB_BASE);
        ADC_enableConverter(ADCC_BASE);

        // delay to allow ADCs to power up
        DEVICE_DELAY_US(1000U);
        // configure the SOCs
        // ISENA - A2
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_EPWM1_SOCA, ADC_CH_ADCIN2, ADC_SAMPLE_WINDOW_2);//IU

        // ISENB - B2
        ADC_setupSOC(ADCB_BASE, ADC_SOC_NUMBER1, ADC_TRIGGER_EPWM1_SOCA, ADC_CH_ADCIN2, ADC_SAMPLE_WINDOW_2);//IV

        // ISENC - C2
        ADC_setupSOC(ADCC_BASE, ADC_SOC_NUMBER2, ADC_TRIGGER_EPWM1_SOCA, ADC_CH_ADCIN2, ADC_SAMPLE_WINDOW_2);//IW

        // VSENA - A1
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER3, ADC_TRIGGER_EPWM1_SOCA, ADC_CH_ADCIN1, ADC_SAMPLE_WINDOW_2);//V

        // VSENB - A10
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER4, ADC_TRIGGER_EPWM1_SOCA, ADC_CH_ADCIN10, ADC_SAMPLE_WINDOW_2);//TEMP
        Bsp_ADC_Interrupt_Init();
}


void Bsp_ADC_Interrupt_Init(void)
{
    // enable the PIE interrupts associated with the ADC interrupts
        Interrupt_register(INT_ADCA1, adca1_isr);
        Interrupt_enable(INT_ADCA1);//(INT_ADCB1);     INT_ADCA1   //RB2
        ADC_enableInterrupt(ADCA_BASE, ADC_INT_NUMBER1);
        ADC_setInterruptSource(ADCA_BASE, ADC_INT_NUMBER1, ADC_SOC_NUMBER3);
}
float Bsp_Get_Iu_Adc_Value(void)
{
    return (float)(AdcaResultRegs.ADCRESULT0);
}
float Bsp_Get_Iv_Adc_Value(void)
{
    return (float)(AdcbResultRegs.ADCRESULT1);
    }
float Bsp_Get_Iw_Adc_Value(void)
{
    return (float)(AdccResultRegs.ADCRESULT2);
    }
float Bsp_Get_Vbus_Adc_Value(void)
{
    return (float)(AdcaResultRegs.ADCRESULT3);
    }
float Bsp_Get_Temp_Adc_Value(void)
{
    return (float)(AdcaResultRegs.ADCRESULT4);
}


